/*
 *  Copyright (C) void Team 2010 Pedro Batista
 * 
 *  http://code.google.com/p/capacitec
 * 
 *  This file is part of capacitec.
 * 
 *  capacitec is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  capacitec is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with capacitec.  If not, see <http://www.gnu.org/licenses/>.
 */
package controller;

import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import entidades.Pessoa;
import seguranca.Usuario;

/**
 *
 * @author pedro
 */
@Resource
public class UsuarioController {

	private final Usuario usuario;
	private final Result result;

	/**
	 *
	 * @param usuario
	 * @param result
	 */
	public UsuarioController(Usuario usuario, Result result) {
		this.usuario = usuario;
		this.result = result;
	}

	/**
	 * Loga uma pessoa ao sistema, e redireciona para index.
	 * @param pessoa pessoa a ser logada ao sistema.
	 */
	@Post
	@Path("/login")
	public void login(Pessoa pessoa) {
		try {
			usuario.login(pessoa);
		} catch (Exception ex) {
			result.redirectTo(ErroController.class).formulario(ex);
			return;
		}
		result.redirectTo(HomeController.class).index();
	}

	/**
	 * Faz logout da pessoa do sistema.
	 */
	@Path("/logout")
	public void logout() {
		if (usuario.isLogado()) {
			usuario.logout();
		}
		result.redirectTo(HomeController.class).index();
	}
}
